<?php


namespace app\admin\controller;


use app\model\ModelUserTask;
use app\service\TaskService;
use easyadmin\app\columns\lists\ListDateTime;
use easyadmin\app\columns\lists\ListText;
use easyadmin\app\libs\ListField;
use easyadmin\app\libs\Page;
use easyadmin\app\libs\PageList;
use think\db\Query;
use think\Model;

class ReviewUserTask extends Admin
{
    protected $pageName = '用户任务审核';
    protected $tableName = 'user_task';
    protected $disabledAction = ['add', 'edit', 'show', 'delete'];

    protected function configListJoin(Page $page, Query $query, string $alias)
    {
        $query->join('user', 'user.id='.$alias.'.user_id');
        $query->join('task','task.id='.$alias.'.task_id');
    }
    protected function configListWhere(Page $page, Query $query, $alias)
    {
        $query->where([
            'user_task.task_status' => 2 //1:已领取未开始; 2: 提交审核; 3:已领取已完成; 4 已取消;
        ]);
    }

    protected function configListField(ListField $list)
    {
        $list
            ->addField('id', 'ID', ListText::class)
            ->addField('user_id', '用户ID', ListText::class)
            ->addField('user.real_name', '用户名', ListText::class)
            ->addField('task.task_no', '任务编码', ListText::class)
            ->addField('task.task_lv', '任务等级', ListText::class)
            ->addField('task.task_type', '任务类型', ListText::class,[
                'class' => 'task-type',
                'jsFiles' => ['/lib/index.js']
            ])
            ->addField('task.task_name', '任务名称', ListText::class)
            ->addField('task_start_time', '开始时间', ListDateTime::class)
            ;
    }

    protected function configList(PageList $page)
    {
        $page
            ->addAction('审核通过', 'reviewTask?type=3',[
                'confirm' => '确定审核通过',
            ])
            ->addAction('审核驳回', 'reviewTask?type=4',[
                'class' => 'layui-btn layui-btn-warm',
                'confirm' => '确定审核驳回',
            ])
        ;
        $page
            ->setOrderBy(["id"=>"desc"])
            ->setTemplate("list:list")
            ;
        $this->data['review'] = "review";
        $this->data['typeY'] = 3;
        $this->data['typeN'] = 4;
    }

    public function reviewTask()
    {
        $input = input();
        $id = trim($input['id']);
        $type = trim($input['type']);
        $userId = (new ModelUserTask())->find($id)['user_id'];
        if(empty($userId)) return $this->error('查不到用户id');
        $res = TaskService::instance()->checkUserCompleteTaskById($id, $type, $userId);
        if($res['status']) {
            return $this->success($res);
        }else{
            return $this->error($res);
        }
    }

    public function reviewBatch(){
        $input = input();
        $ids = trim($input['ids']);
        $type = trim($input['type']);
        if(empty($ids)) {
            return $this->error("参数为空");
        }
        $idArr = explode(",", $ids);
        foreach ($idArr as $id) {
            $userId = (new ModelUserTask())->find($id)['user_id'];
            if(empty($userId)) return $this->error('查不到用户id');
            $res = TaskService::instance()->checkUserCompleteTaskById($id, $type, $userId);
        }
        if($res['status']) {
            return $this->success($res);
        }else{
            return $this->error($res);
        }
    }

    private function log(array $data){
        if(empty($data)) {
            return $this->error("");
        }
    }
}